package com.controller;

import com.entity.Doctor;
import com.entity.Patient;
import com.service.DoctorService;
import com.service.PatientService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        String checkcode = (String) session.getAttribute("CKECKCODE");
        int result = 3; //校验码不正确
        String type = request.getParameter("loginType");/**获取登录人员类型**/
        String account = request.getParameter("uaccount");
        String pwd = request.getParameter("upwd");
        String code = request.getParameter("checkcode");


        /**等待前端传值
         * 由下拉框赋值
         * type接受下拉框
         * **/
        //测试用type
        //type = "patient";

        if (code.equals(checkcode)) {
            if (type.equals("patient")) {
                Patient patient = new Patient(account, pwd);
                PatientService patientService = new PatientService();
                result = patientService.patientValidateLoginInfo(patient);
                if (result == 1) {
                    session.setAttribute("paccount", account);
                    response.sendRedirect("patientServlet?operation=homePage");
                } else {
                    request.setAttribute("error_code", result);
                    request.getRequestDispatcher("loginFailure.jsp").forward(request, response);
                }
            } else if (type.equals("doctor")) {
                Doctor doctor = new Doctor(account, pwd); //医生的工号就是account
                DoctorService doctorService = new DoctorService();
                result = doctorService.doctorValidateLoginInfo(doctor);
                if (result == 1) {
                    session.setAttribute("daccount", account);
                    response.sendRedirect("doctorServlet?skip=mainPage");
                } else {
                    request.setAttribute("error_code", result);
                    request.getRequestDispatcher("loginFailure.jsp").forward(request, response);
                }
            }
            else if (type.equals("none")){
                result = 4;
                request.setAttribute("error_code", result);
                request.getRequestDispatcher("loginFailure.jsp").forward(request, response);
            }

        } else {
            request.setAttribute("error_code", result);
            request.getRequestDispatcher("loginFailure.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
